﻿using System.Linq;
using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Workshop;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.Domain.RBAC;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Workshop
{
    /// <summary>
    /// 获取车间管理信息处理类
    /// </summary>
    public class GetWorkShopHandler : IRequestHandler<GetWorkShopCommand, ApiPaging<WorkShopDto>>
    {
        private readonly IBaseRespository<Workshopmodel> shopRepo;
        private readonly IBaseRespository<UserModel> userRepo;
        private readonly IBaseRespository<Work_order> workRepo;
        private readonly IBaseRespository<BOMInfoModel> bomRepo;
        private readonly IBaseRespository<WorkShopOrder> workshopRepo;

        public GetWorkShopHandler(IBaseRespository<Workshopmodel> shopRepo, IBaseRespository<UserModel> userRepo,
            IBaseRespository<Work_order> workRepo,
            IBaseRespository<BOMInfoModel> bomRepo,
            IBaseRespository<WorkShopOrder> workshopRepo)
        {
            this.shopRepo = shopRepo;
            this.userRepo = userRepo;
            this.workRepo = workRepo;
            this.bomRepo = bomRepo;
            this.workshopRepo = workshopRepo;
        }

        public Task<ApiPaging<WorkShopDto>> Handle(GetWorkShopCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<WorkShopDto> res = new ApiPaging<WorkShopDto>();
            var shops = shopRepo.GetAll().Where(x => !x.Isdel);
            var users = userRepo.GetAll().Where(x => !x.Isdel);
            var work= workRepo.GetAll().Where(x => !x.Isdel);
            var zjb = workshopRepo.GetAll().Where(x => !x.Isdel);


            //编码
            if (!string.IsNullOrEmpty(request.Workshopcode))
            {
                shops = shops.Where(x=>x.Workshopcode.Contains(request.Workshopcode));
            }

            //名称
            if (!string.IsNullOrEmpty(request.Workshopname))
            {
                shops = shops.Where(x => x.Workshopname.Contains(request.Workshopname));
            }

            //描述
            if (!string.IsNullOrEmpty(request.Workshopdesc))
            {
                shops = shops.Where(x => x.Workshopdesc.Contains(request.Workshopdesc));
            }

            //创建人
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                shops = shops.Where(x => x.CreateName.Contains(request.CreateName));
            }

            //创建时间
            if (request.Createkstime!=null && request.Createjstime!=null)
            {
                shops = shops.Where(x => x.CreateTime.AddDays(1)>=request.Createkstime && x.CreateTime<= request.Createjstime);
            }

            //更新时间
            if (request.UpdateksTime != null && request.UpdatejsTime != null)
            {
                shops = shops.Where(x => x.UpdateTime>= request.UpdateksTime && x.UpdateTime<=request.UpdatejsTime);
            }

            var linq = from a in shops
                       join b in zjb on a.Id equals b.WorkshopId
                       join c in work on b.WorkorderId equals c.Id
                       join d in users on a.UserId equals d.Id
                       select new WorkShopDto
                       {
                           Id = a.Id,
                           ShopNum=a.ShopNum,
                           CreateName=a.CreateName,
                           CreateTime=a.CreateTime,
                           Name=d.Name,
                           Order_no = c.Order_no,
                           Status = c.Status,
                           UpdateTime =a.UpdateTime  ,
                           UserId=a.UserId,
                           Workshopcode=a.Workshopcode  ,
                           Workshopdesc=a.Workshopdesc,
                           Workshopname=a.Workshopname,
                       };

            res.PageDatas = linq.OrderByDescending(x=>x.CreateTime).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            res.TotalCount = linq.Count();
            res.Msg = "查询成功";
            res.Code=ApiEnum.Success;
            return Task.FromResult(res);
        }
    }
}
